package com.ruoyi.business.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.business.domain.vo.PeopleRegisterVo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusPersonMapper;
import com.ruoyi.business.domain.BusPerson;
import com.ruoyi.business.service.IBusPersonService;
import javax.annotation.Resource;

/**
 * 人员台账Service业务层处理
 * 
 * @author ling
 * @date 2024-07-10
 */
@Service
public class BusPersonServiceImpl implements IBusPersonService {
    @Resource
    private BusPersonMapper busPersonMapper;

    /**
     * 查询人员台账
     *
     * @param id 人员台账主键
     * @return 人员台账
     */
    @Override
    public BusPerson selectBusPersonById(Long id) {
        return busPersonMapper.selectBusPersonById(id);
    }

    /**
     * 查询人员台账列表
     *
     * @param busPerson 人员台账
     * @return 人员台账
     */
    @Override
    public List<BusPerson> selectBusPersonList(BusPerson busPerson) {

        return busPersonMapper.selectBusPersonList(busPerson);
    }

    /**
     * 新增人员台账
     *
     * @param busPerson 人员台账
     * @return 结果
     */
    @Override
    public int insertBusPerson(BusPerson busPerson) {



            busPerson.setCreateUser(SecurityUtils.getUserId());
            busPerson.setCreateBy(SecurityUtils.getUsername());
            busPerson.setUpdateUser(SecurityUtils.getUserId());
            busPerson.setUpdateBy(SecurityUtils.getUsername());

        busPerson.setCreateTime(DateUtils.getNowDate());
        busPerson.setUpdateTime(DateUtils.getNowDate());
        return busPersonMapper.insertBusPerson(busPerson);
    }

    /**
     * @param busPerson 人员
     * @return
     */
    @Override
    public int register(BusPerson busPerson) {
        busPerson.setCreateTime(DateUtils.getNowDate());
        busPerson.setUpdateTime(DateUtils.getNowDate());
        return busPersonMapper.insertBusPerson(busPerson);
    }

    /**
     * 修改人员台账
     *
     * @param busPerson 人员台账
     * @return 结果
     */
    @Override
    public int updateBusPerson(BusPerson busPerson) {
        busPerson.setUpdateUser(SecurityUtils.getUserId());
        busPerson.setUpdateBy(SecurityUtils.getUsername());
        busPerson.setUpdateTime(DateUtils.getNowDate());
        return busPersonMapper.updateBusPerson(busPerson);
    }

    /**
     * 批量删除人员台账
     *
     * @param ids 需要删除的人员台账主键
     * @return 结果
     */
    @Override
    public int deleteBusPersonByIds(Long[] ids) {
        return busPersonMapper.deleteBusPersonByIds(ids);
    }

    /**
     * 删除人员台账信息
     *
     * @param id 人员台账主键
     * @return 结果
     */
    @Override
    public int deleteBusPersonById(Long id) {
        return busPersonMapper.deleteBusPersonById(id);
    }

    /**
     * 功能描述：检测该班组管理是否被选择
     *
     * @param ids
     * @return int
     * @author ling
     * @date 2024/07/11 09:47
     */
    @Override
    public List<BusPerson> checkBusTeamById(Long[] ids) {
        return busPersonMapper.checkBusTeamById(ids);
    }

    /**
     * 功能描述：获取人员注册量
     *
     * @param begin
     * @param end
     * @return com.ruoyi.business.domain.vo.PeopleRegisterVo
     * @author ling
     * @date 2024/07/11 09:47
     */
    @Override
    public PeopleRegisterVo getTableData(LocalDate begin, LocalDate end) {
        // 获取时间范围内每天的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> numList = new ArrayList<>();

        for (LocalDate date : dateList
        ) {
            // 查询每日的注册量
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            Integer num = busPersonMapper.getTableData(map);
            numList.add(num);
        }

        return PeopleRegisterVo.
                builder()
                .dateList(StringUtils.join(dateList, ','))
                .numList(StringUtils.join(numList, ','))
                .build();
    }

    @Override
    public Integer getCount() {
        return busPersonMapper.getCount();
    }

    @Override
    public String importUser(List<BusPerson> userList, boolean updateSupport, String operName) {

        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BusPerson user : userList) {
            try {
                // 验证是否存在这个用户
                BusPerson c = busPersonMapper.selectBusPersonByPhone(user.getPersonPhone());

                if (StringUtils.isNull(c)) {
                    //  用户未注册过
                    user.setPersonName(user.getPersonName());
                    System.out.println("客户名字："  + user.getPersonName());
                    this.insertBusPerson(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 客户名称:" + user.getPersonName() + " 导入成功 ");
                } else if (updateSupport) {
                    user.setUpdateBy(operName);
                    this.updateBusPerson(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "客户信息 " + user.getPersonName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "客户信息" + user.getPersonName() + " 已存在");
                }
//                user.setCreateBy(operName);
//                this.insertCustomer(user);
//                successNum++;
//                successMsg.append("<br/>" + successNum + "、账号 " + user.getCustomerName() + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "客户信息 " + user.getPersonName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                System.out.println("msg---------------------------------------------------------:" + msg + e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 按团队id获取人员列表
     *
     * @param id 班组id
     * @return
     */
    @Override
    public List<BusPerson> getPersonListByTeamId(Long id) {
        return busPersonMapper.getPersonListByTeamId(id);
    }

    /**
     * 按岗位id获取岗位人员列表
     *
     * @param postId
     * @return
     */
    @Override
    public List<BusPerson> getPostPersonListByPostId(Long postId) {
        return busPersonMapper.getPostPersonListByPostId(postId);
    }
}

